package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.Executive;
import com.example.demo.service.impl.ExecutiveServiceImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/executive")
public class ExecutiveController {

    private final ExecutiveServiceImpl executiveService;


    public ExecutiveController(ExecutiveServiceImpl executiveService) {
        this.executiveService = executiveService;
    }

    /**
     * 新增高管信息
     *
     * @param executive 高管信息
     * @return 是否成功
     */
    @RequestMapping("/add")
    public String addExecutiveInfo(Executive executive) {

        Boolean addJudge = executiveService.addExecutiveInfo(executive);
        if (addJudge) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 修改高管信息
     * 企业编码 高管编码 登记日期 不修改
     *
     * @param executive 高管详细信息
     * @param object    JSON数据
     * @return 是否成功
     */
    @RequestMapping("/update")
    public String updateExecutiveInfo(Executive executive, @RequestBody JSONObject object) {

        String str = object.toJSONString();
        JSONObject jsonObject = JSON.parseObject(str);

        Executive executiveBean = new Executive();
        executiveBean.setExecutiveName(jsonObject.getString("executiveName"));
        executiveBean.setExecutivePosition(jsonObject.getString("executivePosition"));
        executiveBean.setExecutiveGender(jsonObject.getString("executiveGender"));
        executiveBean.setEduBackground(jsonObject.getString("eduBackground"));
        executiveBean.setBornDate(jsonObject.getDate("bornDate"));
        executiveBean.setUpdateDate(jsonObject.getDate("updateDate"));
        executiveBean.setCompanyCode(jsonObject.getString("companyCode"));

        Boolean updateJudge = executiveService.updateExecutiveInfo(executiveBean);
        if (updateJudge) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 删除高管信息
     *
     * @param code 需要删除的高管编号
     * @return 是否成功
     */
    @RequestMapping("/delete")
    public String deleteExecutiveInfoByECode(String code) {

        Integer deleteJudge = executiveService.deleteExecutiveByECode(code);
        if (deleteJudge != 0) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 查询所有高管信息
     *
     * @return 所有高管信息
     */
    @RequestMapping(value = "/selectAll/{code}")
    public List<Executive> selectExecutiveInfoByCompanyCode(@PathVariable String code) {

        List<Executive> executivesList = null;

        try {
            executivesList = executiveService.selectAllExecutiveInfoByCompanyCode(code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (executivesList != null || executivesList.size() != 0) {
            return executivesList;
        }else{

            return null;
        }



    }

    /**
     * 查询高管详细信息
     *
     * @param code 高管编号
     * @return 高管详细信息
     */
    @RequestMapping("/select/{code}")
    public Executive selectExecutiveInfoByECode(@PathVariable String code) {
        return executiveService.selectExecutiveInfoByECode(code);

    }


}
